<?php
/**
 * created by zzy
 * date: 2018/1/26 14:04
 */
namespace App\Eloquent\Ygt;
use Illuminate\Database\Eloquent\SoftDeletes;

class BuyersAddress extends DbEloquent
{
    use SoftDeletes;
    protected $table    = 'ygt_buyers_address';
    protected $dates = ['deleted_at'];
    public static $tableFieldList   = [
        ['name'=>'id','title'=>'','value'=>0,'is_must'=>0],
        ['name'=>'company_id','title'=>'登录异常','value'=>0,'is_must'=>1],
        ['name'=>'buyers_id','title'=>'单位','value'=>0,'is_must'=>1],
        ['name'=>'consignee','title'=>'名称','value'=>'','is_must'=>1],
        ['name'=>'phone','title'=>'手机号','value'=>'','is_must'=>1],
        ['name'=>'province_id','title'=>'所在地区1','value'=>0,'is_must'=>1],
        ['name'=>'city_id','title'=>'所在地区2','value'=>0,'is_must'=>1],
        ['name'=>'area_id','title'=>'所在地区3','value'=>0,'is_must'=>1],
        ['name'=>'address','title'=>'详细地址','value'=>'','is_must'=>1],
        ['name'=>'province_name','title'=>'所在地区4','value'=>'','is_must'=>1],
        ['name'=>'city_name','title'=>'所在地区5','value'=>'','is_must'=>1],
        ['name'=>'area_name','title'=>'所在地区6','value'=>'','is_must'=>1],
        ['name'=>'is_default','title'=>'默认','value'=>0,'is_must'=>0],
    ];
    public static function edit($data,$pk='')
    {
        $id                 = isset($data['id']) ? intval($data['id']) : 0;
        $isDefault          = isset($data['is_default']) ? intval($data['is_default']) : 0;
        $buyersId           = isset($data['buyers_id']) ? intval($data['buyers_id']) : 0;
        if($isDefault && $buyersId)
        {
            $where          = ['buyers_id'=>$buyersId];
            $updateData     = ['is_default'=>0];
            self::where($where)->update($updateData);
        }
        if($id>0){
            $where          = ['id'=>$id];
            $result         = self::updateOneData($where,$data,$pk);
        } else {
            $result         = self::insertOneData($data,$pk);
        }
        return $result;
    }
}
